package org.mch.start;

import java.io.File;
import java.io.FileInputStream;
import java.net.URL;
import java.sql.Connection;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.dbutils.QueryRunner;
import org.mch.comm.db.handler.MapCol1Col2Handler;
import org.mch.comm.itf.IMCHLog;
import org.mch.comm.loging.MComLogger;

public class MLoadDede {
	private Map<Object, Object> dedeHeader;
	private Map<Object, Object> dedeParam;

	private IMCHLog log = new MComLogger(getClass());

	public IMCHLog log() {
		return log;
	}

	public MLoadDede() {
		super();
		try {
			buildDataFromProp();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public MLoadDede(Connection conn) {
		super();
		try {
			buildDataFromDBMS(conn);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void buildDataFromProp() throws Exception {
		dedeHeader = new Properties();
		dedeParam = new Properties();
		URL url1 = MCHWelcome.class.getResource("/resource/ddata/dedeparam.properties");
		URL url2 = MCHWelcome.class.getResource("/resource/ddata/dedeheader.properties");
		((Properties) dedeParam).load(new FileInputStream(new File(url1.getFile())));
		((Properties) dedeHeader).load(new FileInputStream(new File(url2.getFile())));
	}

	private void buildDataFromDBMS(Connection conn) throws Exception {
		String header_sql = "SELECT paramname,paramvalue FROM m_consparam WHERE paramtype='dede_addArticle_requestHeader'";
		String param_sql = "SELECT paramname,paramvalue FROM m_consparam WHERE paramtype='dede_addArticle_requestParam'";
		QueryRunner run = new QueryRunner();

		dedeHeader = run.query(conn, header_sql, new MapCol1Col2Handler());
		dedeParam = run.query(conn, param_sql, new MapCol1Col2Handler());
	}

	public Map<Object, Object> getDedeHeader() {
		return dedeHeader;
	}

	public Map<Object, Object> getDedeParam() {
		return dedeParam;
	}

}
